package com.codeiy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codeiy.criteria.CriteriaQuery;
import com.codeiy.dto.R;
import com.codeiy.entity.data.DataDict;
import com.codeiy.entity.data.DataDictItem;
import com.codeiy.service.DataDictItemService;
import com.codeiy.service.DataDictService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/dict")
public class DataDictController {
    private final DataDictService dataDictService;
    private final DataDictItemService dataDictItemService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return DataDict
     */
    @GetMapping("/{id:\\d+}")
    public R<DataDict> getById(@PathVariable Long id) {
        return R.ok(dataDictService.getById(id));
    }

    /**
     * 查询分页信息
     *
     * @param criteriaQuery 查询参数
     * @return 分页对象
     */
    @PostMapping("/page")
    public R<IPage<DataDict>> page(@RequestBody CriteriaQuery criteriaQuery) {
        return R.ok(dataDictService.page(criteriaQuery.toMybatisPlusPage(), criteriaQuery.toQueryWrapper()));
    }

    /**
     * 添加
     *
     * @param dataDict 实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> save(@RequestBody DataDict dataDict) {
        return R.ok(dataDictService.save(dataDict));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id:\\d+}")
    public R<Boolean> removeById(@PathVariable Long id) {
        return R.ok(dataDictService.removeById(id));
    }

    /**
     * 编辑
     *
     * @param dataDict 实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> update(@RequestBody DataDict dataDict) {
        return R.ok(dataDictService.updateById(dataDict));
    }

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return DataDictItem
     */
    @GetMapping("/item/{id:\\d+}")
    public R<DataDictItem> getItemById(@PathVariable Long id) {
        return R.ok(dataDictItemService.getById(id));
    }

    /**
     * 查询列表信息
     *
     * @return 列表对象
     */
    @PostMapping("/item/list")
    public R<List<DataDictItem>> listItem(@RequestBody CriteriaQuery criteriaQuery) {
        return R.ok(dataDictItemService.list(criteriaQuery.toQueryWrapper()));
    }

    /**
     * 添加
     *
     * @param dictItem 实体
     * @return success/false
     */
    @PostMapping("/item")
    public R<Boolean> saveItem(@RequestBody DataDictItem dictItem) {
        return R.ok(dataDictItemService.save(dictItem));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/item/{id:\\d+}")
    public R<Boolean> removeItemById(@PathVariable Long id) {
        return R.ok(dataDictItemService.removeById(id));
    }

    /**
     * 编辑
     *
     * @param dictItem 实体
     * @return success/false
     */
    @PutMapping("/item")
    public R<Boolean> updateItem(@RequestBody DataDictItem dictItem) {
        return R.ok(dataDictItemService.updateById(dictItem));
    }

    /**
     * 根据编码查询表信息
     *
     * @param dictCode 字典编码
     * @return
     */
    @GetMapping("/details/{dictCode}")
    public R<DataDict> user(@PathVariable String dictCode) {
        DataDict condition = new DataDict();
        condition.setDictCode(dictCode);
        return R.ok(dataDictService.getOne(new QueryWrapper<>(condition)));
    }
}
